<?php
namespace plugins;

/**
 * 工具类
 */
class CommonUntil{
    /**
     * 导出到excel
     * @param $expTitle:第一行的标题
     * @param $expHeadArr:头部数组
     * @param $expDataList:列表数组
     * @return array|string|string[]:文件url地址
     * @throws \PHPExcel_Exception
     * @throws \PHPExcel_Reader_Exception
     * @throws \PHPExcel_Writer_Exception
     */
    public function exportExcelUntil($expTitle,$expHeadArr,$expDataList){
        //设置保存路径
        $expFileName = md5(date('YmdHis')).".xls";
        $basePath = 'upload';
        $baseUrl  = DIRECTORY_SEPARATOR .implode(DIRECTORY_SEPARATOR, ['excel',date('Ymd')]). DIRECTORY_SEPARATOR;
        $path = $basePath.$baseUrl;

        //路径不存在则创建路径
        if(!is_dir($path)){
            mkdir($path, 0777, true);
        }
        $path .= $expFileName;


        $objPHPExcel=new \PHPExcel();
        /*--------------设置表头信息------------------*/
        //设置第一行（标题）合并单元格
        $key = ord('A');
        $num = count($expHeadArr);
        $lastHead = chr($key+$num);
        $mergeCells = 'A1:'.$lastHead.'1';
        $objPHPExcel->getActiveSheet()->mergeCells($mergeCells);
        //单元格居中、字体大小、粗体
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal('center');
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(20);
        $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
        //设置表头信息
        $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1',$expTitle);
        foreach($expHeadArr as $head){
            $colum = chr($key);
            $objPHPExcel->getActiveSheet()->setCellValue($colum.'2',$head);
            $objPHPExcel->getActiveSheet()->getStyle($colum.'2')->getAlignment()->setHorizontal('center');  //表头文字居中
            $key += 1;
        }

        //写入数据
        $colum = 3;
        foreach($expDataList as $key =>$row){//行写入
            $span = ord("A");
            foreach($expDataList[$key] as $keyName => $value){//列写入
                $objPHPExcel->getActiveSheet()->setCellValue(chr($span).$colum,$value);
                $objPHPExcel->getActiveSheet()->getStyle(chr($span).$colum)->getAlignment()->setHorizontal('left');  //文字左对齐中
                $objPHPExcel->getActiveSheet()->getColumnDimension(chr($span))->setWidth(13);//设置宽度
                $span ++;
            }
            $colum ++;
        }
        $objPHPExcel->setActiveSheetIndex(0);
        ob_end_clean();//清除缓冲区,避免乱码

        /**接下来就是设置导入表的名称等内容了**/
        $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
        //保存路径
        $PHPWriter->save($path);
        return str_replace('\\','/',$baseUrl.$expFileName);
    }


    /**
     * 数组转树结构
     * @param $list
     * @param string $pk 当前节点
     * @param string $pid 父节点
     * @param string $child 孩子节点
     * @return array
     */
    static function arrayToTree($list, $pk, $pid, $child = 'children')
    {
        $tree = array();
        if (!is_array($list)) {
            return $tree;
        }
        $refer = array();
        $parentNodeIdArr = [];
        foreach ($list as $key => $data) {
            $refer[$data[$pk]] = &$data;
            $parentNodeIdArr[$data[$pid]] = $data[$pid];
        }
        /* 寻找根结点 */
        foreach ($list as $key => $data) {
            if (in_array($data[$pk], $parentNodeIdArr, false)) {
                unset($parentNodeIdArr[$data[$pk]]);
            }
        }
        foreach ($list as $key => $data) {
            $parentId = $data[$pid];
            if (in_array($parentId, $parentNodeIdArr, false)) {
                $tree[] = &$data;
            } else {
                if (isset($refer[$parentId])) {
                    $parent = &$refer[$parentId];
                    $parent[$child][] = &$list[$key];
                }
            }
        }
        return $tree;
    }
}
